package com.ssm.service;

import com.ssm.dao.IUserDao;
import com.ssm.model.User;
import com.ssm.unity.UUIDUnity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by xieyingze on 2017/12/30.
 */

@Service("UserService")
public class UserServiceImpl implements UserService {

    @Autowired
    IUserDao Mapper;

    /**
     * 用户是否注册
     * @param user_phone 用户手机号
     * @return 用户是否已注册
     */
    public boolean isRegisted(String user_phone) {
        User user = Mapper.queryUserByUserPhone(user_phone);
        if (user != null) {
            return true;
        }
        return false;
    }

    /**
     * 用户登录
     * @param user_phone 用户手机号
     * @param user_password 用户手机号
     * @return 是否登录成功
     */
    public boolean isLogin(String user_phone, String user_password) {
        User user = Mapper.queryUserByUserPhone(user_phone);
        if (user != null) {
            System.out.println("查询出来的账号:" + user.getUser_phone() + "密码:" + user.getUser_password());
            if (user.getUser_phone().equals(user_phone) && user.getUser_password().equals(user_password)) {
                return true;
            }
        }

        return false;
    }

    /**
     * 注册向数据库插入User
     * @param user
     * @return
     */
    public boolean insertUserSuccess(User user) {

        user.setUser_uuid(UUIDUnity.tranformToUUID(user.getUser_phone()));
        user.setUser_registerDate(new Date());

        int status = Mapper.insertUser(user);
        if (status > 0) {
            return true;
        }
        return false;
    }

    /**
     * 根据用户手机号码获取user_uuid
     * @param user_phone
     * @return
     */
    public String queryUserUUIDByUserPhone(String user_phone) {
        User user = Mapper.queryUserByUserPhone(user_phone);
        return user.getUser_uuid();
    }

    /**
     *
     * @param user
     * @return
     */
    public boolean updateLoginInfo(User user) {
        user.setUser_loginDate(new Date());
        List<User> listUser = new ArrayList<User>();
        listUser.add(user);
       int value = Mapper.updateLoginInfo(listUser);
        if (value > 0) {
            return true;
        } else {
            return false;
        }
    }
}
